<?php

class Cms_users extends CI_Model 
{	
	public function auth_user($username,$password)
	{
		$user = $this->db->get_where('su-tb_sys_users',array('strUser'=>$username))->result_array();
		
		$user = current($user);
		
		if(empty($user))
		{
			return "userError";
		}
		else
		{
			//if($user['strCMSUserPassword'] == md5($password))
			if(pw_unhash($user['strPass']) == $password)
			{
				$ip = $this->input->ip_address();
				
				$this->db
					 ->where('intID',$user['intID'])
					 ->update('su-tb_sys_users',array('dteLastLogin'=>date('Y-m-d H:i:s'),'strIpAddress'=>$ip));
					 
				return $user['intID'];									
			}
			else
			{
				return "passwordError";
			}
		}
		
		
	
	}
	
	public function session_init($userid)
	{
		$user = $this->db->get_where('su-tb_sys_users',array('intID'=>$userid))->result_array();
		$user = current($user);
		
		//initialize session
		
		foreach($user as $key=>$value)
		{
			$this->session->set_userdata($key,$value);			
		}
		$this->session->set_userdata('admin_logged_in',1);
		return true;
		
	}
	
	public function get_user_data($userid)
	{
		$user = $this->db->get_where('su-tb_sys_users',array('intID'=>$userid))->result_array();
		$user = current($user);
		return $user;
	}
	
	public function logout_user($userid)
	{
		$user = $this->db->get_where('su-tb_sys_users',array('intID'=>$userid))->result_array();
		$user = current($user);
		
		//de-initialize user's session
		
		foreach($user as $key=>$value)
		{
			$this->session->unset_userdata($key);			
		}
		$this->session->unset_userdata('admin_logged_in');
        
	}
	
	
	
} // end class
?>